package com.spontaneappnew.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.spontaneappnew.NavDrawerActivityConfiguration;
import com.spontaneappnew.NavDrawerItem;
import com.spontaneappnew.NavMenuFBHeader;
import com.spontaneappnew.NavMenuItem;
import com.spontaneappnew.R;
import com.spontaneappnew.adapters.NavDrawerAdapter;

public class SettingsActivity extends AbstractNavDrawerActivity {
	
		// State
		private TextView currRadiusTextView;
		private Switch kmOrMile;
		private ImageButton doneButton;
		private SharedPreferences settings;
		private SharedPreferences.Editor editor;
		private int startRadius, currRadius;
		private String tempRadius;
		private SeekBar seekbar;
		boolean tempIsKm;

		@Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
			
			settings = getSharedPreferences("PREF", 0);
			editor = settings.edit();
			
			currRadiusTextView = (TextView) findViewById(R.id.settings_show_current_radius_text_view);
			kmOrMile = (Switch) findViewById(R.id.settings_switch);
			kmOrMile.setTextOn("Km");
			kmOrMile.setTextOff("Miles");
			doneButton = (ImageButton) findViewById(R.id.settings_done_button);
			seekbar = (SeekBar)findViewById(R.id.settings_seekbar);
			
			startRadius = currRadius = settings.getInt("radius", 20);
			tempIsKm = settings.getBoolean("isKm", true);
			
			seekbar.setProgress(startRadius);
			tempRadius = "" + startRadius;
			currRadiusTextView.setText(tempRadius);
			// update seekbar values to fit KM or Miles
			updateRadiusesAndSeekBar(tempIsKm);
			
			kmOrMile.setChecked(tempIsKm);
			
			/*
			 * Listen to changes in the Switch and update accordingly
			 */
			kmOrMile.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			    	tempIsKm = isChecked;
			    	// update the relevant radiuses
			    	updateRadiusesAndSeekBar(isChecked);
			    }
			});
			
			/*
			 * Listen to changes in the SeekBar and update the current radius TextView accordingly
			 */
			OnSeekBarChangeListener seekListener = new OnSeekBarChangeListener() {
				
				@Override
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					currRadiusTextView.setText("" + progress);
					currRadius = progress;
				}
				
				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {}
				
				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {}
				
			}; 
			seekbar.setOnSeekBarChangeListener(seekListener);
			
			/*
			 * when done button is pressed all we need to do is go back to last fragment
			 */
			doneButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					boolean isRadiusChanged = false;
					// check if km/miles was changed since last time, if so, reload all places
					boolean startKmOrMile = settings.getBoolean("isKm", true);
					// check if radius is not 0
					if(currRadius == 0){
						Toast.makeText(SettingsActivity.this, "Sorry, we can't find places in this radius",  Toast.LENGTH_SHORT).show();
						return;
					}
					if(tempIsKm != startKmOrMile){
						isRadiusChanged = true;
						reloadPlacesInstanceFromSettings();
					}
					/*
					 * update Km or Miles
					 */
					if(tempIsKm){
						editor.putBoolean("isKm", true);
					}
					else{
						editor.putBoolean("isKm", false);
					}
					// update radius id it was changed
					if(startRadius != currRadius){
						editor.putInt("radius", currRadius);
						isRadiusChanged = true;
					}
					
					if(isRadiusChanged){
						reloadPlacesInstanceFromSettings();
					}
					editor.commit();
					finish();
				}
			});
		}
		
		// Changes the value of the boolean sharedPrefs to true so next time the home activity will start it'll update
		public void reloadPlacesInstanceFromSettings(){
			editor.putBoolean("isRadiusChanged", true);
		}

		@Override
	    protected void onNavItemSelected(int id) {
			Intent intent = null;
	        switch ((int)id) {
	        // Home nav button was pressed
	        case 101:
	        	intent = new Intent(this, HomeActivity.class);
	            break;
	        // My places nav drawer was pressed
	        case 102:
	        	intent = new Intent(this, MyPlacesNew.class);
	        	break;
	        // Settings nav button was pressed - just close drawer
	        case 103:
	            break;
		    }
	        if(intent != null)
	        	startActivity(intent);
	        else
	        	super.onNavItemSelected(id);
	    }
		
		private void updateRadiusesAndSeekBar(boolean isKm){
			if(isKm){
				seekbar.setMax(100);
			}
			else{
				seekbar.setMax(62);
			}
		}

		@Override
		protected NavDrawerActivityConfiguration getNavDrawerConfiguration() {
			NavDrawerItem[] menu = new NavDrawerItem[] {
	        		NavMenuFBHeader.create( 100, "User"),
	                NavMenuItem.create(101, "Home", "ic_home", this),
	                NavMenuItem.create(102, "My Places", "ic_menu_myplaces", this),
	                NavMenuItem.create(103, "Settings", "ic_menu_settings", this),
	                NavMenuItem.create(105, "About", "ic_action_about_light", this),
	                NavMenuItem.create(107, "Rate Us!", "ic_whats_hot", this),
	                NavMenuItem.create(108, "Logout", "ic_menu_logout", this)};
	        
	        NavDrawerActivityConfiguration navDrawerActivityConfiguration = new NavDrawerActivityConfiguration();
	        navDrawerActivityConfiguration.setMainLayout(R.layout.activity_settings);
	        navDrawerActivityConfiguration.setDrawerLayoutId(R.id.drawer_layout);
	        navDrawerActivityConfiguration.setLeftDrawerId(R.id.left_drawer);
	        navDrawerActivityConfiguration.setNavItems(menu);
	        navDrawerActivityConfiguration.setDrawerShadow(R.drawable.drawer_shadow);       
	        navDrawerActivityConfiguration.setDrawerOpenDesc(R.string.drawer_open);
	        navDrawerActivityConfiguration.setDrawerCloseDesc(R.string.drawer_close);
	        navDrawerActivityConfiguration.setBaseAdapter(
	            new NavDrawerAdapter(this, R.layout.navdrawer_item, menu ));
	        return navDrawerActivityConfiguration;
		}
}